package com.tarena.elts.ui;

import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.tarena.elts.entity.User;
import com.tarena.elts.service.ExamService;
import com.tarena.elts.service.IdOrPwdException;

public class ClientContext {
	private LoginFrame loginFrame;
	private MenuFrame menuFrame;
	private ExamService examService;
	private WelcomeWindow welcomeWindow;
	
	
	public void setLoginFrame(LoginFrame loginFrame) {
		this.loginFrame = loginFrame;
	}

	public void setWelcomeWindow(WelcomeWindow welcomeWindow) {
		this.welcomeWindow = welcomeWindow;
	}

	public void setMenuFrame(MenuFrame menuFrame) {
		this.menuFrame = menuFrame;
	}

	public void setExamService(ExamService examService) {
		this.examService = examService;
	}

	public void login(){
		try {
			int id = loginFrame.getUserId();
			String pwd = loginFrame.getPwd();
			User user = examService.login(id, pwd);
			menuFrame.updateView(user);
			loginFrame.setVisible(false);
			menuFrame.setVisible(true);
		} catch (IdOrPwdException e) {
			// TODO: handle exception
			e.printStackTrace();
			loginFrame.showMessage(e.getMessage());
		}

	}
	public void showUI(){
		this.loginFrame.setVisible(true);
	}
	public void exit(JFrame form){
		int val = JOptionPane.showConfirmDialog(form,"exit");
		if(val==JOptionPane.YES_OPTION){
			System.exit(0);
		}
	}
}
